<template>
  <PageWrapper>
    <n-card :bordered="false" class="mb-3 proCard">
      <n-space align="center">
        <n-input-group style="width: 980px">
          <div style="margin-left: 10px !important;display: flex;align-items: center;">
            <div style="width: 160px;">名称：</div>
            <n-input
              style="width: 100%;margin-left: 20px;"
              v-model:value="params.title"
              clearable
              placeholder="请输入名称"
              @keyup.enter="reloadTable"
            />
          </div>

          <n-button type="primary" style="margin-left: 40px !important;" @click="reloadTable">
            <template #icon>
              <n-icon>
                <SearchOutlined />
              </n-icon>
            </template>
            查询
          </n-button>
        </n-input-group>
        
      </n-space>
    </n-card>
    <n-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="basicTableRef"
        :actionColumn="actionColumn"
        @update:checked-row-keys="onCheckedRow"
        virtual-scroll
      >
        <template #tableTitle>
          <n-space align="center">
            <n-button type="primary" @click="openCreateDrawer">
              <template #icon>
                <n-icon>
                  <FileAddOutlined />
                </n-icon>
              </template>
              添加
            </n-button>
          </n-space>
        </template>
      </BasicTable>
    </n-card>

    <CreateDrawer
      ref="createDrawerRef"
      :title="drawerTitle"
      :roleList="roleData"
      @change="reloadTable"
    />
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { h, reactive, ref, onMounted } from 'vue';
  import { BasicColumn, BasicTable, TableAction } from '@/components/Table';
  import { boxlist } from '@/api/device/type';
  
  import { FileAddOutlined, SearchOutlined } from '@vicons/antd';
  import CreateDrawer from './CreateDrawer.vue';
  import { columns } from './columns';

  const basicTableRef = ref();
  const rowKeys = ref([]);
  const rowKeysName = ref([]);
  const tableData = ref();
  const createDrawerRef = ref();
  const drawerTitle = ref('添加类型');
  const roleData = ref([]);

  const params = reactive({
    title: '',
  });

  const actionColumn: BasicColumn = reactive({
    width: 150,
    title: '操作',
    key: 'action',
    fixed: 'right',
    align: 'center',
    render(record) {
      return h(TableAction as any, {
        actions: [
          {
            label: '编辑',
            onClick: handleEdit.bind(null, record),
          },
        ],
      });
    },
  });

  const loadDataTable = async (res) => {
    const result = await boxlist({ ...params, ...res });
    tableData.value = result.list;
    return result;
  };

  function onCheckedRow(keys) {
    rowKeys.value = keys;
    rowKeysName.value = tableData.value
      .filter((item) => {
        return keys.includes(item.id);
      })
      .map((item) => {
        return item.username;
      })
      .join('，');
  }

  function reloadTable() {
    basicTableRef.value.reload();
  }

  function openCreateDrawer() {
    const { openDrawer } = createDrawerRef.value;
    openDrawer();
  }

  function handleEdit(record: Recordable) {
    drawerTitle.value = '编辑类型';
    const { openDrawer } = createDrawerRef.value;
    openDrawer(record.id);
  }

  onMounted(async () => {
    
  });
</script>

<style lang="less" scoped></style>
